<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 分类管理
 */
class IndexController extends Controller{

    /**
     * status 状态
     *首页显示
     */
    public function index(){  //------------------------excel 表导入    分页数据
        header('Access-Control-Allow-Origin:http://hc.jswl.cc');
        //设置字符集
        $check_r     = getone('check_url','jsadmin.admin_check',"",'id');//二维码
  
        $new_serve_r = getone('newname,createtime','jsadmin.admin_new_server',"",'id');//新加服务器
        $new_serve_r[0]['createtime'] = date('Y-m-d',$new_serve_r[0]['createtime']);
        
        for($i=1;$i<5;$i++){
            $status['status'] = $i;
            $list_image_r[$i] = getorder('*','jsadmin.admin_list_image',$status,'createtime');//图鉴  status 1 一星 2 二星 3三星 4 四星
        }
        $rotation = getorder('*','jsadmin.admin_rotation',0,'createtime');//图鉴  status 1 一星 2 二星 3三星 4 四星

        for($i=1;$i<4;$i++){
            $status['status']   = $i;
            $new_activity_r[$i] = getlimit('id,createtime,status,title','jsadmin.admin_journalism',$status,'createtime',0,5);//新闻 status 1 最新 2 新闻 3 公告
        }

        $array['serve']     = array_filter($new_serve_r);
        $array['check']     = array_filter($check_r);
        $array['img']       = array_filter($list_image_r);
        $array['activity']  = array_filter($new_activity_r);
        $array['rotation']  = array_filter($rotation);

        $data = array('result' => 1, 'msg' =>$array);
        echo json_encode($data);die;
    }

    /**
     *status  1 最新 2 新闻 3 公告
     * 新闻 最新
     */
    public function newJournalism(){//根据传值status 返回数据 分页
         //设置字符集
        header('Access-Control-Allow-Origin:http://hc.jswl.cc');
        $model=M('journalism');//浅复制一个模型
        $where['status']   = (int)1;
        $count = $model->where($where)->count();//连惯操作后会对join等操作进行重置
      //为保持在为定的连惯操作，浅复制一个模型
        $p=new \Think\Page($count,10);
        $p->lastSuffix=false;
        $p->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录  每页<b>%LIST_ROW%</b>条  第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $p->setConfig('prev','上一页');
        $p->setConfig('next','下一页');
        $p->setConfig('last','末页');
        $p->setConfig('first','首页');
        $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

        $p->parameter=I('get.p');
        $new_journalism_r= getlimit('id,createtime,status,title','jsadmin.admin_journalism',$where,'createtime',$p->firstRow,$p->listRows);
        //$new_journalism_r = $model->where($where)->limit($p->firstRow,$p->listRows);
        //var_dump($new_journalism_r);die;
        $data = array('result' => 1, 'msg' =>$new_journalism_r,'page'=>$p);
        echo json_encode($data);die;

    }
    /**
     * 新闻页面  status  1 最新 2 新闻 3 公告
     */
    public function journalism(){//根据传值status 返回数据 分页
        //设置字符集
        header('Access-Control-Allow-Origin:http://hc.jswl.cc');

        $model=M('journalism');//浅复制一个模型
        $where['status']   = (int)2;
        $count = $model->where($where)->count();//连惯操作后会对join等操作进行重置
      //为保持在为定的连惯操作，浅复制一个模型
        $p=new \Think\Page($count,10);
        $p->lastSuffix=false;
        $p->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录  每页<b>%LIST_ROW%</b>条  第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $p->setConfig('prev','上一页');
        $p->setConfig('next','下一页');
        $p->setConfig('last','末页');
        $p->setConfig('first','首页');
        $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

        $p->parameter=I('get.p');
        $new_journalism_r= getlimit('id,createtime,status,title','jsadmin.admin_journalism',$where,'createtime',$p->firstRow,$p->listRows);
        //$new_journalism_r = $model->where($where)->limit($p->firstRow,$p->listRows);
        //var_dump($new_journalism_r);die;
        $data = array('result' => 1, 'msg' =>$new_journalism_r,'page'=>$p);
        echo json_encode($data);die;


    }
    /**
     * 新闻 公告    status  1 最新 2 新闻 3 公告
     */
    public function noticeJournalism(){//根据传值status 返回数据 分页
        //设置字符集
        header('Access-Control-Allow-Origin:http://hc.jswl.cc');

        $model=M('journalism');//浅复制一个模型
        $where['status']   = (int)3;
        $count = $model->where($where)->count();//连惯操作后会对join等操作进行重置
      //为保持在为定的连惯操作，浅复制一个模型
        $p=new \Think\Page($count,10);
        $p->lastSuffix=false;
        $p->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录  每页<b>%LIST_ROW%</b>条  第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $p->setConfig('prev','上一页');
        $p->setConfig('next','下一页');
        $p->setConfig('last','末页');
        $p->setConfig('first','首页');
        $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

        $p->parameter=I('get.p');
        $new_journalism_r= getlimit('id,createtime,status,title','jsadmin.admin_journalism',$where,'createtime',$p->firstRow,$p->listRows);
        //$new_journalism_r = $model->where($where)->limit($p->firstRow,$p->listRows);
        //var_dump($new_journalism_r);die;
        $data = array('result' => 1, 'msg' =>$new_journalism_r,'page'=>$p);
        echo json_encode($data);die;


    }
    /**
     * 1 游戏活动
     * 
     */
    public function activity(){//根据传值status 返回数据 分页
        //设置字符集
        header('Access-Control-Allow-Origin:http://hc.jswl.cc');

        $model=M('new_activity');//浅复制一个模型
        $where['status']   = (int)1;
        $count = $model->where($where)->count();//连惯操作后会对join等操作进行重置
      //为保持在为定的连惯操作，浅复制一个模型
        $p=new \Think\Page($count,10);
        $p->lastSuffix=false;
        $p->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录  每页<b>%LIST_ROW%</b>条  第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $p->setConfig('prev','上一页');
        $p->setConfig('next','下一页');
        $p->setConfig('last','末页');
        $p->setConfig('first','首页');
        $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

        $p->parameter=I('get.p');
        $new_journalism_r= getlimit('*','jsadmin.admin_new_activity',$where,'createtime',$p->firstRow,$p->listRows);
        //$new_journalism_r = $model->where($where)->limit($p->firstRow,$p->listRows);
        //var_dump($new_journalism_r);die;
        $data = array('result' => 1, 'msg' =>$new_journalism_r,'page'=>$p);
        echo json_encode($data);die;
            
    }
    /**
     * 2 游戏攻略
     * 
     */
    public function strategy(){//根据传值status 返回数据 分页

                 //设置字符集
       header('Access-Control-Allow-Origin:http://hc.jswl.cc');

        $model=M('new_activity');//浅复制一个模型
        $where['status']   = (int)2;
        $count = $model->where($where)->count();//连惯操作后会对join等操作进行重置
      //为保持在为定的连惯操作，浅复制一个模型
        $p=new \Think\Page($count,10);
        $p->lastSuffix=false;
        $p->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录  每页<b>%LIST_ROW%</b>条  第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $p->setConfig('prev','上一页');
        $p->setConfig('next','下一页');
        $p->setConfig('last','末页');
        $p->setConfig('first','首页');
        $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

        $p->parameter=I('get.p');
        $new_journalism_r= getlimit('*','jsadmin.admin_new_activity',$where,'createtime',$p->firstRow,$p->listRows);
        //$new_journalism_r = $model->where($where)->limit($p->firstRow,$p->listRows);
        //var_dump($new_journalism_r);die;
        $data = array('result' => 1, 'msg' =>$new_journalism_r,'page'=>$p);
        echo json_encode($data);die;
            
    }

    /**
     * 详情页面信息
     */
    public function filedone(){
         //设置字符集
        header('Access-Control-Allow-Origin:http://hc.jswl.cc');
        $model = I('model');
        $id['id'] = I('model_id');
        if(!empty($model) && !empty($id)){
           
            if($model=='jour'){//新闻
                $list = getfield('*','jsadmin.admin_journalism',$id);
            }
            if($model=='activity'){//活动攻略
                $list = getfield('*','jsadmin.admin_new_activity',$id);
            }
            if($model=='img'){
                $list = getfield('*','jsadmin.admin_list_image',$id);
            }
            //var_dump($list);die;
            $data = array('result' => 1, 'msg' =>$list);
            echo json_encode($data);die;
        }else{
            $data = array('result' => 0, 'msg' =>"请求失败!请重新请求！");
            echo json_encode($data);die;
        }
    }



    
    /** 
     * 点击领取码后调用  判断用户领取码
     */
    public function receiveTokey(){
         //设置字符集
       header('Access-Control-Allow-Origin:http://hc.jswl.cc');
       // $user_name['user_name'] = I('user_name');
       $user_name['userid']     = $_SESSION['userid'];
       $user_name['username']   = $_SESSION['username'];
       $user_name['accesstoken']= $_SESSION['accesstoken'];
        if(!empty($user_name['username'])){
            $check = M('gift_check')->where($user_name)->find();
            if($check){
                if($check['status']==1){
                    $data = array('result' => 2, 'msg' =>"您已经领取过礼包兑换码了！ 请不要重复领取！", 'content' =>$check['gift_token']);
                    echo json_encode($data);die;
                }
                if($check['status']==2){
                    $data = array('result' => 3, 'msg' =>"您已经领取过礼包了！ 请不要重复领取！", 'content' =>$check['gift_token']);
                    echo json_encode($data);die;
                }
              
            }else{//选取新手礼品码
                $where['status']  = 0;
                $model = M('gift_check')->where($where)->find();
                $tokey                  = $model['gift_token'];
                $save_id['id']          = $model['id'];
                $user_up['receivetime'] = time();
                $user_up['userid']      = $_SESSION['userid'];
                $user_up['username']    = $_SESSION['username'];
                $user_up['accesstoken'] =  $_SESSION['accesstoken'];
                $user_up['status']      = 1;
                $save = M('gift_check')->where($save_id)->save($user_up);
                if($save){
                    $data = array('result' => 1, 'msg' =>"领取礼品兑换码成功！",'content' =>$tokey);
                    echo json_encode($data);die;
                }else{
                    $data = array('result' => 4, 'msg' =>"领取礼品兑换码失败!请重新请求！");
                    echo json_encode($data);die;
                }
            }

        }else{
            $data = array('result' => 0, 'msg' =>"请求失败!请重新请求！");
            echo json_encode($data);die;
        }
    }

    // public function where(){//编辑器
    //     $rr = $_SERVER['DOCUMENT_ROOT'];
    //     // $tt = M('new_activity');
    //     // $cont = $tt->where('status=1')->find();
    //     // $str = str_replace('<img src="','<img src="http://192.168.1.63',$cont);
    //     // <img src="
    //     var_dump($rr);die;
    // }


    public function crossDomain(){//跨域请求
        header('Access-Control-Allow-Origin:http://hc.jswl.cc');
        $url = 'http://sdk.api.jswl.cc/v3/api/login/password';
        $name['username']           = trim(I('username'));
        $name['password']           = trim(I('password'));
        $name['gamePackage']        = 'com.tdxd.JSLogin';
        $name['os']                 = 'ios';
        $name['osVersion']          = 'ios 7.0';
        $name['uniqueDeviceCode']   = '123456';
        $name['phoneModel']         = 'iphone x';
        $name['client_id']          = 'ios';
        $name['client_secret']      = 'jingshi-sdk-ios';
        $port=80;//一般是80 可不写
        if(!empty($name['username']) && !empty($name['password'])){
            $cont = $this->api_get($url,$name,$port);
            $content = json_decode($cont,true);

            session_start();
            $_SESSION['userid'] = $content['data']['userId'];
            $_SESSION['username'] = $content['data']['username'];
            $_SESSION['accesstoken'] = $content['data']['accessToken'];
            if($content['message']=="登录成功"){
                $data = array('result' => 1, 'msg' =>$content['message'].'!');
                echo json_encode($data);die;
            }else{
                $data = array('result' => 0, 'msg' =>$content['message'].'!');
                echo json_encode($data);die;
            }
        }else{
            $data = array('result' => 0, 'msg' =>"账号或密码错误!请重新请求！");
            echo json_encode($data);die;
        }
        
    }

    public function api_get($url = '', $post_data = array(), $port=80) {
    
        if (empty($url) || empty($post_data)) {
            return false;
        }

        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $postUrl = $url;
        $curlPost = $post_data;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_PORT,$port);//指定连接端口
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }
}


        
